package view
{
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import model.CharacterPx;
	import model.UserPx;
	import vo.CharacterVO;
	import manager.ResourceMgr;
	
	/**
	 * -- TeamView --
	 * @desc	view to build teams
	 * @author	Aqua (built on 2011-11-09)
	 */
	public class TeamView extends BaseView
	{
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * -- TeamView --
		 * @desc		constructor
		 * @param		main view object of this view class
		 *************************************************************************/
		public function TeamView(viewComponent:DisplayObjectContainer):void
		{
			super(viewComponent);
			
			_viewComponent.name = VIEW_NAME;
			_userPx = getPx(UserPx.NAME) as UserPx;
			_charaPx = getPx(CharacterPx.NAME) as CharacterPx;
			_currentPage = 1;
			_dragCache = {};
			_posIconList = [];
			
			addAddCmd("txt_name",			add_txt_name);
			addAddCmd("img_icon",			add_img_icon);
			addAddCmd("txt_captain",		add_txt_captain);
			addAddCmd("txt_pos",			add_txt_pos);
			addAddCmd("img_icon_pos",		add_img_icon_pos);
			addAddCmd("txt_combination",	add_txt_combination);
			addAddCmd("btn_combination",	add_btn);
			addAddCmd("txt_page",			add_txt_page);
			addAddCmd("btn_left",			add_btn);
			addAddCmd("btn_right",			add_btn);
			
			addClickCmd("btn_combination",	click_btn_combination);
			addClickCmd("btn_left",			click_btn_left);
			addClickCmd("btn_right",		click_btn_right);
		}
		
		
		//------------------//
		//     Privates     //
		//------------------//
		
		//-- Add Handlers --//
		private function add_txt_name():void
		{
			if (! charaExists())
			{
				return;
			}
			
			TextField(_display).text = _charaPx.getCharaName(getCharaVO().id);
		}
		private function add_img_icon():void
		{
			var id:String;
			var type:String;
			var name:String;
			var childName:String;
			var i:int;
			var container:DisplayObjectContainer;
			
			container = DisplayObjectContainer(_display);
			
			if (! charaExists())
			{
				if (_display.hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					_display.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				}
				
				cleanContainer(container, 1);
				return;
			}
			
			id = getCharaVO().id;
			type = ResourceMgr.instance.CHARA;
			name = id + "_sm";
			childName = type + "/" + name;
			
			if (getChild(DisplayObjectContainer(_display), childName))
			{
				return;
			}
			
			cleanContainer(container, 1);
			ResourceMgr.instance.loadImg(type, name, container);
			
			if (! _display.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				_display.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			}
		}
		private function add_txt_captain():void
		{
			
		}
		private function add_txt_pos():void
		{
			var txt:TextField;
			
			txt = TextField(_display);
			txt.mouseEnabled = false;
			
			switch (getPos())
			{
				case _charaPx.FRONT:
					txt.text = "前";
					break;
				case _charaPx.MIDDLE:
					txt.text = "中";
					break;
				case _charaPx.BACK:
					txt.text = "后";
					break;
				default:
					txt.text = "";
					break;
			}
		}
		private function add_img_icon_pos():void
		{
			var index:int;
			var id:String;
			var type:String;
			var name:String;
			var childName:String;
			var i:int;
			var container:DisplayObjectContainer;
			var charaVO:CharacterVO;
			
			index = getIndex(_display);
			_posIconList[index] = _display.parent;
			container = DisplayObjectContainer(_display);
			charaVO = _charaPx.getCharaByPos(index);
			
			if (! charaVO)
			{
				if (_display.hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					_display.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				}
				
				cleanContainer(container, 2);
				return;
			}
			
			id = charaVO.id;
			type = ResourceMgr.instance.CHARA;
			name = id + "_sm";
			childName = type + "/" + name;
			
			if (getChild(DisplayObjectContainer(_display), childName))
			{
				return;
			}
			
			cleanContainer(container, 2);
			ResourceMgr.instance.loadImg(type, name, container);
			
			if (! _display.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				_display.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			}
		}
		private function add_txt_combination():void
		{
			
		}
		private function add_txt_page():void
		{
			TextField(_display).text = String(_currentPage) + "/" + String(getPageNum());
		}
		private function add_btn():void
		{
			makeBtn(_display);
		}
		
		//-- Click Handlers --//
		private function click_btn_combination():void
		{
			
		}
		private function click_btn_left():void
		{
			if (_currentPage > 1)
			{
				_currentPage --;
				onRefresh();
			}
		}
		private function click_btn_right():void
		{
			if (_currentPage < getPageNum())
			{
				_currentPage ++;
				onRefresh();
			}
		}
		
		
		/**************************************************************************
		 * -- getCharaVO --
		 * @desc		get character's data in current display object
		 * @return 		character's data
		 *************************************************************************/
		private function getCharaVO():CharacterVO
		{
			var index:int;
			var charaVO:CharacterVO;
			
			index = getIndex(_display) + NUM_PER_PAGE * (_currentPage - 1);
			charaVO = _charaPx.getCharaByIndex(index);
			
			return charaVO;
		}
		
		/**************************************************************************
		 * -- getPos --
		 * @desc		get current display object's position
		 * @return		position
		 *************************************************************************/
		private function getPos():int
		{
			var index:int;
			var pos:int;
			
			index = getIndex(_display);
			pos = (index < 4 ? _charaPx.FRONT : (index < 8 ? pos = _charaPx.MIDDLE : _charaPx.BACK));
			
			return pos;
		}
		
		/**************************************************************************
		 * -- charaExists --
		 * @desc		check if character exists in current display object's position
		 * @return		exists or not
		 *************************************************************************/
		private function charaExists():Boolean
		{
			var index:int;
			
			index = getIndex(_display) + NUM_PER_PAGE * (_currentPage - 1);
			
			return index < _charaPx.charaNum;
		}
		
		/**************************************************************************
		 * -- getPageNum --
		 * @desc		get total number of pages
		 * @return		number of pages
		 *************************************************************************/
		private function getPageNum():int
		{
			return int(_charaPx.charaNum / NUM_PER_PAGE) + 1;
		}
		
		/**************************************************************************
		 * -- onMouseDown --
		 * @desc		event handler on mouse down
		 * @param		(e) event information
		 *************************************************************************/
		private function onMouseDown(e:MouseEvent):void
		{
			var icon:Sprite;
			
			icon = Sprite(e.target);
			_dragCache.charaIndex = getIndex(icon) + NUM_PER_PAGE * (_currentPage - 1);
			_dragCache.icon = icon;
			_dragCache.x = icon.x;
			_dragCache.y = icon.y;
			
			if (! icon.stage.hasEventListener(MouseEvent.MOUSE_UP))
			{
				icon.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			}
			
			_viewComponent.setChildIndex(icon.parent, _viewComponent.numChildren - 1);
			icon.startDrag();
		}
		
		/**************************************************************************
		 * -- onMouseUp --
		 * @desc		event handler on mouse up
		 * @param		(e) event information
		 *************************************************************************/
		private function onMouseUp(e:MouseEvent):void
		{
			var iconPos:Point;
			var icon:Sprite;
			
			e.target.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			if (_dragCache.icon)
			{
				icon = Sprite(_dragCache.icon);
				iconPos = new Point(_dragCache.icon.x, _dragCache.icon.y);
				iconPos = icon.parent.localToGlobal(iconPos);
				checkOverlap(iconPos);
				icon.stopDrag();
				_dragCache.icon.x = _dragCache.x;
				_dragCache.icon.y = _dragCache.y;
				_dragCache = {};
			}
			
			function checkOverlap(pos:Point):void
			{
				var index:String;
				var posIcon:DisplayObject;
				
				for (index in _posIconList)
				{
					posIcon = _posIconList[index];
					if (posIcon && posIcon.hitTestPoint(pos.x, pos.y))
					{
						_charaPx.setCharaPos(_dragCache.charaIndex, int(index));
						onRefresh();
						break;
					}
				}
			}
		}
		
		/**************************************************************************
		 * -- cleanContainer --
		 * @desc		remove children from a container
		 * @param		(container) container
		 * @param		(numLeft) amount of children those needn't to be removed
		 *************************************************************************/
		private function cleanContainer(container:DisplayObjectContainer, numLeft:int):void
		{
			var i:int;
			
			i = container.numChildren - 1;
			while (i > (numLeft - 1))
			{
				container.removeChildAt(i);
				i --;
			}
		}
		
		
		public static const VIEW_NAME	:String = "TeamView";
		public static const LAYER_INDEX	:int	= 2;
		
		private const NUM_PER_PAGE	:int = 24;
		
		private var _userPx			:UserPx;
		private var _charaPx		:CharacterPx;
		private var _currentPage	:int;
		private var _dragCache		:Object;
		private var _posIconList	:Array;
	}
	
}